//在老式手机上，用户通过数字键盘输入，手机将提供与这些数字相匹配的单词列表。每个数字映射到0至4个字母。给定一个数字序列，实现一个算法来返回匹配单词的列表。你
//会得到一张含有有效单词的列表。映射如下图所示： 
//
// 
//
// 示例 1： 
//
// 
//输入：num = "8733", words = ["tree", "used"]
//输出：["tree", "used"]
// 
//
// 示例 2： 
//
// 
//输入：num = "2", words = ["a", "b", "c", "d"]
//输出：["a", "b", "c"] 
//
// 提示： 
//
// 
// num.length <= 1000 
// words.length <= 500 
// words[i].length == num.length 
// num中不会出现 0, 1 这两个数字 
// 
//
// Related Topics 数组 哈希表 字符串 👍 41 👎 0


package LeetCode.editor.cn;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ldltd
 * @date 2025-09-10 23:26:45
 * @description 面试题 16.20.T9键盘
 */
public class T9Lcci{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 T9Lcci fun=new T9Lcci();
	 	 Solution solution = fun.new Solution();
//			solution.getValidT9Words("8733", new String[]{"tree", "used"});
			solution.getValidT9Words("2", new String[]{"a", "b", "c", "d"});
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public List<String> getValidT9Words1(String num, String[] words) {
		Map<Character,char[]> dict=Map.of(
				'2', new char[]{'a', 'b', 'c'},
				'3', new char[]{'d', 'e', 'f'},
				'4', new char[]{'g', 'h', 'i'},
				'5', new char[]{'j', 'k', 'l'},
				'6', new char[]{'m', 'n', 'o'},
				'7', new char[]{'p', 'q', 'r', 's'},
				'8', new char[]{'t', 'u', 'v'},
				'9', new char[]{'w', 'x', 'y', 'z'});
		List<String> ans=new ArrayList<>();
		o1:
		for (String word : words) {
			if (word.length() != num.length()) {
				continue;
			}
			boolean flag = true;
			o2:
			for (int i = 0; i < word.length()&&flag; i++) {
				char c = word.charAt(i);
				char[] chars = dict.get(num.charAt(i));
				o3:
				for (char aChar : chars) {
					if(aChar==c) {
						flag=true;
						continue o2;
					}

				}
				flag=false;
			}
			if (flag) {
				ans.add(word);
			}
		}
		return ans;
    }
	public List<String> getValidT9Words(String num, String[] words) {
		List<String> res = new ArrayList<>();
		int length = num.length();
		int[] key = {2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6,
				7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9};
		for (String word: words) {
			int i = 0;
			for (i = 0; i < length; i++) {
				if (num.charAt(i) != key[word.charAt(i) - 'a'] + '0') {
					break;
				}
			}
			if (i >= length)
				res.add(word);
		}
		return res;
	}

}
//leetcode submit region end(Prohibit modification and deletion)

}
